package cn.tedu.boot31.controller;

import cn.tedu.boot31.entity.Product;
import cn.tedu.boot31.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Classname: ProductController
 * @Author: bromide
 * @CreateTime: 2022/5/12--10:08
 * @Version: V1.0
 * @Description:
 */

//此注解相当于在每一个方法上面添加ResponseBody
@RestController
public class ProductController {
    @Autowired(required = false)
    ProductMapper mapper;

    @RequestMapping("/insert")
    public String insert(Product product){
        System.out.println("product:"+product);
        mapper.insert(product);
        return "添加完成!<a href='/index.html'>返回首页</a>";
    }

    @RequestMapping("/select")
    public String select(){
        List<Product> list = mapper.select();
        String html="<table border=1>";
        html+="<caption>商品列表</caption>";
        html+="<tr><th>标题</th><th>价格</th><th>销量</th><th>操作</th></tr>";
        for(Product product:list){
            html+="<tr><th>"+product.getTitle()+"</th><th>"+product.getPrice()+"</th><th>"+product.getSaleCount()+"</th><th><a href='/delete?title="+product.getTitle()+"'>删除</a></th></tr>";
        }
        html+="</table>";
        return html;
    }

    @RequestMapping("/update")
    public String update(Product product){
        mapper.update(product);
        return "修改完成!<a href='/index.html'>返回首页</a>";
    }

    @RequestMapping("/delete")
    public String  delete(String title){
        mapper.delete(title);
        return "删除成功!<a href='/index.html'>返回首页</a>";
    }
}
